<?php
/*
Plugin Name: Saksh WP SMTP 
Version: 1.1.1
Plugin URI: https://www.cleanmailserver.com/
Author: susheelhbti
Author URI: https://www.cleanmailserver.com/
Description: Integrate wordpress to your mandrill , sendgrid , getresponse, clenmailserver SMTP Server, Amazon SES or any SMTP Server.
*/

class options_page {
	function __construct() {
	  $saksh_email_by_smtp = new Saksh_Email_By_SMTP();
		add_action( 'admin_menu', array( $saksh_email_by_smtp, 'add_plugin_page' ) );
	}
	 
}
new options_page;





class Saksh_Email_By_SMTP
{
    /**
     * Holds the values to be used in the fields callbacks
     */
    private $sebso;

    /**
     * Start up
     */
    public function __construct()
    {  $this->sebso = get_option( 'saksh_email_admin' );
    
      // add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'page_init' ) );
    }

    /**
     * Add options page
     */
    
	public function add_plugin_page()
    {	
        // This page will be under "Settings"
        add_options_page(
            'Settings Admin', 
            'Saksh WP SMTP', 
            'manage_options', 
            'saksh_wordpress_smtp', 
            array( $this, 'create_admin_page' )
        );
    }

    /**
     * Options page callback
     */
    public function create_admin_page()
    {
 
        $this->sebso = get_option( 'saksh_email_admin' );
  ?>
        <div class="wrap">
			<H2>Saksh Wordpress SMTP</h2>
   <div id="poststuff">
      <div id="post-body" class="metabox-holder columns-2">          
           <?php echo offer();?> 
		  
                        <div class="postbox">
                        	
                       	 
                          <div class="inside">
		   <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields( 'saksh_email_group' );   
                do_settings_sections( 'saksh_email_admin' );
                submit_button(); 
            ?>
            </form>
			
			  </div> 
        </div> </div> </div></div>
		<style>
		.form-table {
    
    clear: none;
    
}
		</style>
        <?php
    }

    /**
     * Register and add settings
     */
    public function page_init()
    {        
      	 
  register_setting(
            'saksh_email_group', // Option group
            'saksh_email_admin', // Option name
            array( $this, 'sanitize' ) // Sanitize
        );

        add_settings_section(
            'setting_section_id', // ID
            'Provide your SMTP settings here', // Title
            array( $this, 'print_section_info' ), // Callback
            'saksh_email_admin' // Page
        );  
		

	add_settings_field(
            'from_name', // ID
            'From Name', // Title 
            array( $this, 'from_name_callback' ), // Callback
            'saksh_email_admin', // Page
            'setting_section_id' // Section           
        );    
		
		
	add_settings_field(
            'from_email', // ID
            'From Email', // Title 
            array( $this, 'from_email_callback' ), // Callback
            'saksh_email_admin', // Page
            'setting_section_id' // Section           
        );    
 
	add_settings_field(
            'host', // ID
            'SMTP Server', // Title 
            array( $this, 'host_callback' ), // Callback
            'saksh_email_admin', // Page
            'setting_section_id' // Section           
        );      
  add_settings_field(
            'auth', // ID
            'SMTPAuth', // Title 
            array( $this, 'auth_callback' ), // Callback
            'saksh_email_admin', // Page
            'setting_section_id' // Section           
        );      
  add_settings_field(
            'port', // ID
            'Port', // Title 
            array( $this, 'port_callback' ), // Callback
            'saksh_email_admin', // Page
            'setting_section_id' // Section           
        );      
  add_settings_field(
            'smtp', // ID
            'SMTPSecure', // Title 
            array( $this, 'smtpsecure_callback' ), // Callback
            'saksh_email_admin', // Page
            'setting_section_id' // Section           
        );      
  add_settings_field(
            'username', // ID
            'User Name', // Title 
            array( $this, 'username_callback' ), // Callback
            'saksh_email_admin', // Page
            'setting_section_id' // Section           
        );      
  add_settings_field(
            'password', // ID
            'Password', // Title 
            array( $this, 'password_callback' ), // Callback
            'saksh_email_admin', // Page
            'setting_section_id' // Section           
        );      
   
    }		

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
	
	//var_dump($input);
 
        $new_input = array();
        
     
if( isset( $input['from_name'] ) )
            $new_input['from_name'] = sanitize_text_field( $input['from_name'] );

			
if( isset( $input['from_email'] ) )
            $new_input['from_email'] = sanitize_text_field( $input['from_email'] );
			
if( isset( $input['host'] ) )
            $new_input['host'] = sanitize_text_field( $input['host'] );

			
if( isset( $input['auth'] ) )
            $new_input['auth'] = sanitize_text_field( $input['auth'] );
 
if( isset( $input['SMTPAuth'] ) )
            $new_input['SMTPAuth'] = sanitize_text_field( $input['SMTPAuth'] );
			
			
if( isset( $input['port'] ) )
            $new_input['port'] = sanitize_text_field( $input['port'] );
			
	
if( isset( $input['smtpsecure'] ) )
            $new_input['smtpsecure'] = sanitize_text_field( $input['smtpsecure'] );

			
						
if( isset( $input['username'] ) )
            $new_input['username'] = sanitize_text_field( $input['username'] );
			
			
if( isset( $input['password'] ) )
            $new_input['password'] = sanitize_text_field( $input['password'] );
			
					
			
        return $new_input;
    }

    /** 
     * Print the Section text
     */
    public function print_section_info()
    {
   //     print 'Enter your settings below:';
    }

     
     
	
	/** 
     * Get the settings option array and print one of its values
     */
    public function from_name_callback()
    {
        printf(
            '<input type="text" id="from_name" name="saksh_email_admin[from_name]" value="%s" />',
            isset( $this->sebso['from_name'] ) ? esc_attr( $this->sebso['from_name']) : 'cleanmailserver'
        );
    }
	 	/** 
     * Get the settings option array and print one of its values
     */
    public function from_email_callback()
    {
        printf(
            '<input type="text" id="from_email" name="saksh_email_admin[from_email]" value="%s" />',
            isset( $this->sebso['from_email'] ) ? esc_attr( $this->sebso['from_email']) : ''
        );
    }
	 
	/** 
     * Get the settings option array and print one of its values
     */
    public function host_callback()
    {
        printf(
            '<input type="text" id="host" name="saksh_email_admin[host]" value="%s" />',
            isset( $this->sebso['host'] ) ? esc_attr( $this->sebso['host']) : 'smtp.cleanmailserver.com'
        );
    }
	
	
	/** 
     * Get the settings option array and print one of its values
     */
    public function auth_callback()
    {
        printf(
            '<input type="text" id="auth" name="saksh_email_admin[auth]" value="%s" />( yes / no)',
            isset( $this->sebso['auth'] ) ? esc_attr( $this->sebso['auth']) : 'yes'
        );
    }
	
		/** 
     * Get the settings option array and print one of its values
     */
    public function port_callback()
    {
        printf(
            '<input type="text" id="port" name="saksh_email_admin[port]" value="%s" />',
            isset( $this->sebso['port'] ) ? esc_attr( $this->sebso['port']) : '587'
        );
    }
		/** 
     * Get the settings option array and print one of its values
     */
    public function smtpsecure_callback()
    {
        printf(
            '<input type="text" id="smtpsecure" name="saksh_email_admin[smtpsecure]" value="%s" />( ssl / tls / none)',
            isset( $this->sebso['smtpsecure'] ) ? esc_attr( $this->sebso['smtpsecure']) : 'tls'
        );
    }	
	
	
	
	/** 
     * Get the settings option array and print one of its values
     */
    public function username_callback()
    {
        printf(
            '<input type="text" id="username" name="saksh_email_admin[username]" value="%s" />',
            isset( $this->sebso['username'] ) ? esc_attr( $this->sebso['username']) : ''
        );
    }
	
	/** 
     * Get the settings option array and print one of its values
     */
    public function password_callback()
    {
        printf(
            '<input type="password" id="password" name="saksh_email_admin[password]" value="%s" />',
            isset( $this->sebso['password'] ) ? esc_attr( $this->sebso['password']) : ''
        );
    }
	
	
}

if( is_admin() )
    $saksh_email_by_smtp = new Saksh_Email_By_SMTP();
	
	
 
add_action( 'phpmailer_init', 'saksh_settings' );




function saksh_settings( $phpmailer ) {


       $saksh = get_option( 'saksh_email_admin' );
		
		
    $phpmailer->isSMTP();     
	
	$phpmailer->FromName = $saksh['from_name'];
    $phpmailer->From = $saksh['from_email'];
	
	
    $phpmailer->Host = $saksh['host'];
    $phpmailer->SMTPAuth = $saksh['auth'];; // Force it to use Username and Password to authenticate
    $phpmailer->Port =  $saksh['port'];;
	$phpmailer->SMTPSecure  =  $saksh['smtpsecure'];;
    $phpmailer->Username = $saksh['username'];
    $phpmailer->Password = $saksh['password'];
	$phpmailer->XMailer = "Customized for http://www.cleanmailserver.com/";
	//to debug you can un comment this
	//var_dump($phpmailer);
}


   function offer()
   {
   ?>
<div id="postbox-container-1" class="postbox-container">
   <div class="meta-box-sortables">
      <div class="postbox">
         <h3><span><img src="http://www.cleanmailserver.com/wp-content/uploads/2014/05/10516907_664330970315508_1940511407_n-300x37.jpg"    alt="Cleanmailserver SMTP Server" width=80% /></span></h3>
         <div class="inside">
        
            <p><em>
		
		
			
              CleanMailServer is now a robust platform, equipped with the state of the art infrastructure, 24 x 7 dedicated connectivity to the internet, and scalable architecture. </em>
            </p>
            <p><em>For our WordPress Plugin we do offer a huge discount	  	</p>
            <p class="voucher" style="background:#21759B; border-radius: 5px; padding:10px; text-align:center; color:#FFF; line-height:15px;"><strong style="font-size:16px">30% OFF ALL<br />Email Plan Packages*</strong><br />
               Your Promotional Code:<strong style="font-size:32px; background:#FFF; color:#21759B; line-height: 38px; display:block; padding: 2px 0">Saksham</strong>Discount can be applied on Checkout<a class="button" href="http://www.cleanmailserver.com/pricing-plans/" target="_blank">Click Here to Buy Now</a>
            </p><strong style="color: #21759B">
		  Recommanded plan 19.99$ per month <br>unlimited emails 5 000 Subscribers
		   </strong>
		   <a class="button" href="http://www.cleanmailserver.com/checkout/?add-to-cart=4882" target="_blank"> Buy Now</a>
         </div>
      </div>
   </div>
</div>
<?php
}


 
 